﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Pronghorn.Core
{
	public class FileSystemSkinLocator : ISkinLocator
	{
		#region Fields

		private readonly IProngHornControllerContext _context;

		#endregion Fields

		#region Constructors

		public FileSystemSkinLocator(IProngHornControllerContext context)
		{
			_context = context;
		}

		#endregion Constructors

		#region Private Methods

		private string getPathToSkin()
		{
			var skinPath = _context.SiteContext.SkinPath;
			var theme = _context.SiteContext.Theme;
			var controller = _context.RouteData.GetRequiredString("Controller");
			var action = _context.RouteData.GetRequiredString("Action");
			return string.Format("{0}/{1}/{2}/{3}.htm",skinPath,theme,controller,action);
		}

		#endregion Private Methods

		#region Public Methods

		public string GetAssemblyName()
		{
			return getPathToSkin()
			    .Replace(".", "/")
			    .Replace('/', '.')
			    .Replace("htm", "dll");
		}

		public string GetNameSpace()
		{
			return Assembly.GetCallingAssembly().GetName().Name;
		}

		public IList<string> GetSkinLines()
		{
			var skinPath = getPathToSkin();
			if (File.Exists(skinPath))
			{
			    return File.ReadAllLines(skinPath);
			}
			return new[] { "" };
		}

		public string GetSkinType()
		{
			return getPathToSkin()
			    .Replace(".", "")
			    .Replace("htm", "")
			    .Replace('/', '_');
		}

		#endregion Public Methods
	}
}